﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity.Model;
using SystemFrameWork.Enums;

namespace DAL
{
    public class Sys_FunctionModuleDAO : EnittyDAO
    {
        /// <summary>
        /// load the system menu via the sys_user permission
        /// </summary>
        /// <param name="sUser"></param>
        /// <returns></returns>
        public List<LeftMenuTreeView> LoadLeftMenuTree(Sys_User sUser)
        {
            try
            {
                List<LeftMenuTreeView> slmList = new List<LeftMenuTreeView>();
                DataContextManager mgr = new DataContextManager();
                if (sUser.IsSuperAdministrator)
                {
                    List<Sys_FunctionModule> list = mgr.CurrentDataContext.Sys_FunctionModules.Where(l=>l.RecordStatus==(byte)RecordStatus.RecordActive).OrderBy(l=>l.OrderNo).ToList();
                    foreach (Sys_FunctionModule sfm in list)
                    {
                        LeftMenuTreeView lmt = new LeftMenuTreeView();
                        lmt.ID = sfm.ID;
                        lmt.NavigationUrl = sfm.NavigationUrl;
                        lmt.ParentID = sfm.ParentID;
                        lmt.sys_UserID = sUser.ID;
                        lmt.Value = sfm.Value;
                        lmt.FunctionName = sfm.FunctionName;
                        slmList.Add(lmt);
                    }
                }
                else
                {
                    //var query = from fm in mgr.CurrentDataContext.Sys_FunctionModule
                    //            join
                    //            frm in mgr.CurrentDataContext.Sys_FunctionModuleSys_Role
                    //            on fm.ID equals frm.Sys_FunctionModuleID
                    //            join sr in mgr.CurrentDataContext.Sys_Role
                    //            on frm.Sys_RoleID equals sr.ID
                    //            join sru in mgr.CurrentDataContext.Sys_RoleUser
                    //            on sr.ID equals sru.Sys_RoleID
                    //            join u in mgr.CurrentDataContext.Sys_User
                    //            on sru.Sys_UserID equals u.ID
                    //            where u.ID == sUser.ID
                    //            group fm by new
                    //            {
                    //                ID = frm.ID,
                    //                FunctionName = fm.FunctionName,
                    //                NavigationUrl = fm.NavigationUrl,
                    //                Value = fm.Value,
                    //                ParentID = fm.ParentID
                    //            } into g
                    //            select g;

                    slmList = mgr.CurrentDataContext.LeftMenuTreeView.Where(v => v.sys_UserID == sUser.ID).ToList();
                }

                return slmList;
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}
